using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
using popLib;

namespace Kas8Laugher
{
    public partial class Form1 : Form
    {
        IntPtr handle = IntPtr.Zero;
        bool isShow = false;
        

        public Form1()
        {
            InitializeComponent();
            notifyIcon1.Visible = true;            
            
            handle = APILib.FindWindow(null, "Kaspersky Anti-Virus 2009");

            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 100;
            timer.Tick += delegate{ 
            	this.Hide();
            	if( !this.Visible ) timer.Stop();
            };
            timer.Start();
            
            notifyIcon1.MouseClick += delegate(object sender, MouseEventArgs e) { 
            	if( e.Button == MouseButtons.Left )
            		mit_show.PerformClick();
            };
            
            mit_closeKas.Click += delegate { 
            	handle = APILib.FindWindow(null, "Kaspersky Anti-Virus 2009");            	
            	if( handle != IntPtr.Zero ){
            		APILib.SendMessage( handle, (int)APILib.SendMessageCode.WM_COMMAND, (int)APILib.SendMessageCode.WM_CLOSE, 0 );
            	}
            };
            
            mit_show.Click += delegate{ 
            	isShow = !isShow;
            	ShowKas( isShow );
            	mit_show.Text = ( isShow ? "Hide " : "Show " ) + "Kaspersky Antivirus";
            };
            
            mit_exit.Click += delegate { this.Close(); };
            
            mit_close.Click += delegate {
            	mit_closeKas.PerformClick();            	
            	this.Close();
            };
            
            ShowKas( false );
        }
        
        void StartKas(){
        	if( handle != IntPtr.Zero) return;
        	string path = FormLib.getAppSettingINI( "Setting", "KasperskyPath", @"D:\My Documents\Softwares\#Compress Tools\#Kaspersky\KAVPortable\App\KAV8\avp.exe" );
        	while( !File.Exists(path) ){
        		FormLib.alert( "Kaspersky Antivirus execute file is not seted. Please select one." );
        		ReturnSet rs = FormLib.openFile( "Kaspersky application(avp.exe)|avp.exe|Exe files(*.exe)|*.exe" );
        		if( !rs.Success ) return;
        		path = rs.Result.ToString();
        		FormLib.setAppSettingINI( "Setting", "KasperskyPath", path );
        	}
        	ProcessStartInfo info = new ProcessStartInfo();
        	info.FileName = path;
        	info.WorkingDirectory = Path.GetDirectoryName( path );
        	Process.Start( info );
        }
        
        void ShowKas( bool show ){        	
        	handle = APILib.FindWindow(null, "Kaspersky Anti-Virus 2009");
        	if( handle == IntPtr.Zero ) StartKas();
        	
        	int counter = 0;
        	while( handle == IntPtr.Zero ){        		
        		handle = APILib.FindWindow(null, "Kaspersky Anti-Virus 2009");        		
        		System.Threading.Thread.Sleep(1000);
        		counter++;
        		if( counter >= 3 ) return;
        	}
        	
        	if( show ) APILib.ShowWindow( handle, (int)APILib.ShowWindowStyle.Show );
        	else APILib.ShowWindow( handle, (int)APILib.ShowWindowStyle.Hide );
        }
    }
}
